<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     @click.stop="handleGenerate()">生成统计
          </el-button>
          <el-button type="primary"
                     size="small"
		     v-if="false"
                     @click.stop="handleExport()">导 出
          </el-button>
          <el-button type="primary"
                     size="small"
                     :hidden="true"
                     v-if="false"
                     @click.stop="handlePrint()">打 印
          </el-button>
        </template>
        <template slot="menu" slot-scope="{row, index}">
          <el-button type="text" size="small"
            icon="el-icon-view"
            @click="handleEdit(row, index)">编 辑
          </el-button>
          <el-button type="text" size="small"
            icon="el-icon-view"
            @click="handleView(row, index)">预 览
          </el-button>
        </template>
      </avue-crud>
      <open-dialog title="生成统计" :DialogVisible.sync="dialogVisibleGenerate" width="40%">
        <el-form v-if="dialogVisibleGenerate">
          <avue-form ref="generateForm" v-model="dateForm" :option="detailGenerateOption"></avue-form>
        </el-form>
        <div class="bottom-menu-tab" v-if="dialogVisibleGenerate">
          <el-button type="primary" :loading="loading" @click="generate">确认</el-button>
          <el-button type="primary" @click="dialogVisibleGenerate = false"
            >取消</el-button
          >
        </div>
      </open-dialog>
      <open-dialog title="编 辑" :DialogVisible.sync="dialogVisibleEdit">
        <dynamic-crud title="明细" :data="detailEditData"
          :option="detailEditOption" :button="false">
        </dynamic-crud>
        <div class="bottom-menu">
          <el-button type="primary" @click="editSubmit">确认</el-button>
          <el-button type="primary" @click="dialogVisibleEdit = false"
            >取消</el-button
          >
        </div>
      </open-dialog>
      <open-dialog title="预 览" :DialogVisible.sync="dialogVisible">
        <dynamic-crud title="明细" :data="detailData"
          :option="detailOption" :button="false">
        </dynamic-crud>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {getDetailObj,updateDetail,calcOneWayStatistics} from '@/api/subway/swstonewaystatistics'
import {nowDate,detailEditOption,detailGenerateOption} from '@/const/crud/subway/swstonewaystatistics'
import { dateFormat } from "@/util/date"

export default {
  name: 'swstonewaystatistics',
  mixins: [baseCrud('subway/swstonewaystatistics')],
  data() {
    return {
      dialogVisible: false,
      dialogVisibleEdit: false,
      dialogVisibleGenerate: false,
      detailData: [],
      detailEditData: [],
      // searchForm: {
      //   dateTime: nowDate
      // },
      dateForm: {},
      detailEditOption: detailEditOption,
      detailGenerateOption: detailGenerateOption,
      loading: false
    }
  },
  computed: {
    permissionList() {
      return {}
    },
    detailOption(){
      let option = this.deepClone(this.tableOption)
      this.findObject(option.column, 'lineName').hide = false
      return option
    }
  },
  methods: {
    handleGenerate(){
      for (var i in this.dateForm) {
          this.dateForm[i] = null;
      }
      let startTime = new Date();
      let endTime = new Date();
      startTime.setMonth(startTime.getMonth() - 1);
      this.dateForm.startTime = dateFormat(startTime).substr(0, 8)+'26';
      this.dateForm.endTime = dateFormat(endTime).substr(0, 8)+'25';
      this.dialogVisibleGenerate = true;
    },
    async generate(){
      if(dateFormat(new Date(this.dateForm.startTime))>dateFormat(new Date(this.dateForm.endTime))){
        this.$message.warning('开始日期不能大于结束日期!')
      }
      let param = {
        startTime: this.dateForm.startTime,
        endTime: this.dateForm.endTime
      }
      let flag = true;
      this.$refs.generateForm.validate(vaild=>{
        if(!vaild){
          flag = false;
        }
      })
      if(flag){
        this.loading  = true;
        await calcOneWayStatistics(param)
          .then(data => {
            this.$message.success("统计生成成功");
            this.getList(this.page);
            this.loading  = false;
            this.dialogVisibleGenerate = false;
            done();
          })
          .catch(error=> {
            if(error.message.indexOf('timeout')!=-1){
              this.$message.warning('生成需要一定时间，请稍后查看!')
              this.loading = false;
              this.dialogVisibleGenerate = false;
            }
          });
      }
    },
    handleEdit(row, index){
      getDetailObj(row.dateTime).then(response => {
        this.detailEditData = response.data.data;
        for(const item of this.detailEditData){
          item.$cellEdit = true;
        }
        this.dialogVisibleEdit = true;
      })
    },
    editSubmit(){
      updateDetail(this.detailEditData)
        .then(data => {
          this.$message.success("修改成功");
          this.getList(this.page);
          this.dialogVisibleEdit = false;
          done();
        })
        .catch(() => {});
    },
    handleView(row, index){
      getDetailObj(row.dateTime).then(response => {
        this.detailData = response.data.data
        this.dialogVisible = true
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.bottom-menu {
  margin-top: 20px;
  text-align: center;
}
.bottom-menu-tab {
  margin-top: 5px;
  text-align: center;
}
</style>
